<template>
  <Dialog v-model="dialogVisible" title="导入模板文件" width="600">
    <el-form ref="ruleFormRef" :rules="formRules" :model="formData" label-position="right" label-width="auto">
      <el-form-item label="操作类型" :label-width="formLabelWidth" prop="">
        <el-radio-group v-model="formData.operationType" class="ml-4">
          <el-radio label="1">新增</el-radio>
          <el-radio label="2">维护</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="资源类型" :label-width="formLabelWidth" prop="">
        <el-radio-group v-model="formData.resourceType" class="ml-4">
          <el-radio label="1">excel文件</el-radio>
          <el-radio v-show="formData.operationType === '1'" label="2">导入无结构数据文件</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <el-upload class="upload-demo" :http-request="determine" drag action="" multiple :accept="accept()">
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text">
        将文件拖到此处或 <em>单击以上传</em>
      </div>
      <template #tip>
        <div class="el-upload__tip">
        </div>
      </template>
    </el-upload>
    <template #footer>
      <el-button @click="handleDownload" type="primary">下载EXCEL模板</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script lang="ts" setup>
import * as FormApi from '@/api/knowledge/character'
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
// import xlsxFilePath from '@/assets/template/entityTemplateFile.xlsx'
const props = defineProps({
  activeName: String
})
interface FormData {
  operationType: string
  resourceType: string
}
const formData = reactive<FormData>({
  operationType: '1',
  resourceType: '1',
})
const formLabelWidth = '80px'
const formRules = reactive({
  operationType: [{ required: true, message: '知识库类型不能为空', trigger: 'change' }],
  resourceType: [{ required: true, message: '知识库简介不能为空', trigger: 'blur' }]
})

const exp = reactive({
  pathUrl: import.meta.env.VITE_URL
})
const accept = () => {
  if (formData.resourceType === '1') return '.xlsx, .xls'
  else if (formData.resourceType === '2') return '.txt'
  else return '.xlsx, .xls'
}
const router = useRouter()
const route = useRoute()
const dialogVisible = ref(false) // 弹窗的是否展示
const handleDownload = () => {
  let url = ''
  if (props.activeName === '1') {
    url = exp.pathUrl+'/src/assets/template/entityTemplateFile.xlsx'
  }
  else if (props.activeName === '2') {
    url = exp.pathUrl+'/src/assets/template/eventTemplateFile.xlsx'
  }
  console.log(url)
  // 创建一个临时的<a>标签用于下载
  const downloadLink = document.createElement('a');
  downloadLink.href = url;
  downloadLink.download = 'entityTemplateFile.xlsx'; // 指定下载文件名
  document.body.appendChild(downloadLink);
  // 模拟点击下载链接
  downloadLink.click();
  // 清理临时DOM元素和对象URL
  document.body.removeChild(downloadLink);
  URL.revokeObjectURL(url);
}
// 弹窗确定
const determine = (param) => {
  let excelFormData = new FormData()
  excelFormData.append('templateFile', param.file) // 文件
  excelFormData.append('graphId', route.query.graphId) // 所属图谱id
  excelFormData.append('graphName', route.query.graphName) // 所属图谱名
  excelFormData.append('uploadType', props.activeName) // 文件类型 1:人物、2:事件
  const params = excelFormData
  if (formData.operationType === '1') {
    if (formData.resourceType === '1') {
      FormApi.insertHybaseAndNeo4jByExcel(params).then(res => dialogVisible.value = false)
    } else if (formData.resourceType === '2') {
      FormApi.insertHybaseAndNeo4jByStr(params).then(res => dialogVisible.value = false)
    }
  } else if (formData.operationType === '2') {
    if (formData.resourceType === '1') {
      FormApi.readExcelAndUpdateEntity(params).then(res => dialogVisible.value = false)
    }
  }
}
/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>
